public class Oppattachmentcontroller
{
    public List<OpportunityAttachmentWper> lstAttachment{get;set;}
    public string opportunityId{get;set;}
    public Oppattachmentcontroller(ApexPages.StandardController stdController)
        {
        lstAttachment=new List<OpportunityAttachmentWper>();
        Set<Id> setIds=new set<Id>();
        Opportunity opp=(Opportunity)stdController.getRecord();
        opportunityId=opp.Id;
        setIds.add(opportunityId);
        List<Quote> LstQuote=[select Id from Quote where OpportunityId=:opportunityId];
        for(Quote Quote1:LstQuote)
        {
            setIds.add(Quote1.Id);
        }
        List<Order__c> lstOrder=[select id from Order__c where Opportunity__c=:opportunityId]; 
        for(Order__c Order:lstOrder)
        {
            setIds.add(Order.Id);
        }
        Map<string,string> mapattachType=new Map<string,string>();
        List<Opportunity_Attachments_Classification__c> LstoppAttachType=[select id,AttachmentId__c,Attachment_Type__c,Opportunity__c from Opportunity_Attachments_Classification__c where Opportunity__c=:opportunityId];
        for(Opportunity_Attachments_Classification__c oppattType:LstoppAttachType)
        {
        mapattachType.put(oppattType.AttachmentId__c,oppattType.Attachment_Type__c);
        }
        List<Attachment> lstAttachments=[Select Id,ParentId,CreatedByID,LastModifiedById,Parent.Type,Parent.Name,Name,LastModifiedDate,LastModifiedBy.Name,CreatedBy.Name,ContentType From Attachment where ParentId in :setIds];
        for(Attachment attch:lstAttachments)
            {
                OpportunityAttachmentWper attachwper=new OpportunityAttachmentWper();
                attachwper.attachmentId=attch.Id;
                if(mapattachType.containsKey(attch.Id))
                attachwper.AttachmentType=mapattachType.get(attch.Id);
                attachwper.ParentID=attch.ParentId;
                if(attch.Parent.Type=='Order__c')
                    attachwper.Objects ='Fulfillment Order';
                else
                    attachwper.Objects=attch.Parent.Type;
                attachwper.objtype=attch.Parent.Name;
                attachwper.Title=attch.Name;
                attachwper.CreatedByID=attch.CreatedById;
                attachwper.LastModifiedID=attch.LastModifiedById;
                attachwper.LastModifiedBy=attch.LastModifiedBy.Name;
                attachwper.LastModifiedDate=String.valueOf(attch.LastModifiedDate);
                attachwper.CreatedBy=attch.CreatedBy.Name;
                lstAttachment.add(attachwper);

            }
 
        }
    public String getParam(String name) 
    {
        return ApexPages.currentPage().getParameters().get(name);   
    }
    public PageReference del() 
    {
        try 
        {
            String delid = getParam('delid');
            Integer rowNo =Integer.valueOf(getParam('rowNo'));     
            lstAttachment.remove(rowNo);     
            List<Opportunity_Attachments_Classification__c> lstattachclass=[select id from Opportunity_Attachments_Classification__c where AttachmentId__c=:delid];
            delete lstattachclass;
            Attachment attach=[select id from Attachment where id=:delid];
            system.debug('test'+attach);
            delete attach;
            
            system.debug('test'+attach);            
        } catch (Exception e) {
            ApexPages.addMessages(e);
        }
        return null;
    }
    public PageReference edit()
    {
        String editid = getParam('editid');
        PageReference pageref=new PageReference('/'+editid+'/e');
        pageref.setredirect(true);
        return pageref;
    }   
}